// app.ts

import {IAppOption} from './app.option'
import {getWxSetting, getWxUserProfile} from './utils/wx.api'
import {RentalCar} from './service/request'

let resolveUserInfo: (value: WechatMiniprogram.UserInfo | PromiseLike<WechatMiniprogram.UserInfo>) => void
let rejectUserInfo: (reason?: any) => void

App<IAppOption>({
    globalData: {
        userInfo: new Promise((resolve, reject) => {
            resolveUserInfo = resolve
            rejectUserInfo = reject
        })
    },
    async onLaunch() {

        // 微信登录
        await RentalCar.login()

        /*wx.login({
            success: res => {
                console.log(res);
                wx.request({
                    url: "http://localhost:8080/v1/auth/login",
                    method: "POST",
                    data: {
                        code: res.code
                    } as auth.v1.ILoginRequest,
                    success: res => {

                        const loginResp: auth.v1.LoginResponse = auth.v1.LoginResponse
                            .fromObject(camelcaseKeys(res.data as Object));

                        console.log(loginResp)

                        // TODO 我的出行测试
                        wx.request({
                            url: "http://localhost:8080/v1/trip",
                            method: "POST",
                            data: {
                                start: Math.random().toString()
                            } as rental.v1.ICreateTripRequest,
                            header: {
                                authorization: `Bearer ${loginResp.accessToken}`
                            },
                            success: res => {

                                const tripResp: rental.v1.CreateTripResponse = rental.v1.CreateTripResponse
                                    .fromObject(camelcaseKeys(res.data as Object))

                                console.log(tripResp)

                            }
                        })


                    },
                    fail: console.error
                })
            }
        })
        */

        /*  wx.request({
              url: 'http://localhost:8080/trip/test123',
              method: "GET",
              success: res => {
                  const tripResp = car_rental.GetTripResponse.fromObject(
                      camelcaseKeys(res.data as Object,
                          {deep: true}))
                  console.log(tripResp);
                  console.log(car_rental.TripStatus[tripResp.trip?.status!]);
              },
              fail: console.error
          })*
         */

        wx.showModal({
            content: '为了更好的用户体验，我们将获取您的头像信息和名称',
            success: async () => {
                try {
                    // 获取微信配置
                    const settingResp = await getWxSetting()
                    // 查看是否授权
                    if (settingResp.authSetting['scope.userInfo']) {
                        let userProfileResp = await getWxUserProfile()
                        resolveUserInfo(userProfileResp.userInfo)
                    }
                } catch (err) {
                    rejectUserInfo(err)
                }
            }
        })

    },
    resolveUserInfo(userInfo: WechatMiniprogram.UserInfo) {
        resolveUserInfo(userInfo)
    }
})
